/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');
    var toastr =require('toastr');
    app.useModule("ui.table");
    var datepicker = require('datepicker');
    app.controller('account_transferCtrl', ['$scope','$http','$filter' ,function ($scope, $http,$filter) {
        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };
        $scope.ctrlScope=$scope;
        $scope.add={};
        $scope.orderParams = {
            url: "/admin/account/listServiceRebate"
        };
        datepicker($scope);

        //创建
        $scope.addOrderModal=function(){
            $scope.addModal = !$scope.addModal;
            $scope.add={};
        };

        //新建转账
        $scope.addTransferModal=function(){
            $scope.transferModal = !$scope.transferModal;
            $scope.transfer={};
        };
        //id查真实姓名
        $scope.isRegister= function (type,userId) {
            console.info(userId);
            $http({
                method: 'POST',
                url: "/admin/account/getUsernameById",
                data:{userId:userId}
            }).success(function(data) {
                console.info(data);
                if(data.result=='fail'){
                    toastr.error('用户不存在');
                }else{
                    toastr.success('验证成功');
                    if(type==1){
                        $scope.add.fromName=data.username;
                        $scope.transfer.fromName=data.username;
                    }else if(type==2){
                        $scope.add.toName=data.username;
                        $scope.transfer.toName=data.username;
                    }
                }
            }).error(function(data) {});
        };
        //全局查询
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDateTime=$scope.ctrlScope.itemsearch.beginDateTimex;
            $scope.ctrlScope.itemsearch.endDateTime=$scope.ctrlScope.itemsearch.endDateTimex;
            $scope.ctrlScope.itemsearch.beginDateTime=$filter('date')($scope.ctrlScope.itemsearch.beginDateTime,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDateTime=$filter('date')($scope.ctrlScope.itemsearch.endDateTime,'yyyy-MM-dd');
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };
        //全局查询重置
        $scope.reset=function(){
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.ajaxParam={};
            $scope.orderParams.reload();
        };
        //新建
        $scope.adoptAdd = function(isValid) {
            if (!isValid) {
                toastr.error("信息填写有误");
            }
            var parm=$scope.add;
            $http({
                method: 'POST',
                url: "/admin/account/serviceRebate",
                data:parm
            }).success(function(data) {
                console.info(data);
                if(data.result=='fail'){
                    if(data.messageText){
                        toastr.error(data.messageText);
                    }else{
                        toastr.error('新建失败');
                    }
                }else{
                    toastr.success('新建成功');
                    $scope.addModal = !$scope.addModal;
                    $scope.orderParams.reload();

                }
            }).error(function(data) {});
        };
        //新建
        $scope.transferSubmit = function(isValid) {
            if (!isValid) {
                toastr.error("信息填写有误");
            }
            var parm=$scope.transfer;
            $http({
                method: 'POST',
                url: "/admin/account/transfer",
                data:parm
            }).success(function(data) {
                console.info(data);
                if(data.result=='fail'){
                    if(data.messageText){
                        toastr.error(data.messageText);
                    }else{
                        toastr.error('新建失败');
                    }
                }else{
                    toastr.success('新建成功');
                    $scope.transferModal = !$scope.transferModal;
                    $scope.orderParams.reload();

                }
            }).error(function(data) {});
        };
        //新建
        $scope.checkAccount = function() {
            toastr.success('处理中');
            $http({
                method: 'POST',
                url: "/admin/account/checkAccount"
            }).success(function(data) {
                console.info(data);
                if(data.result=='fail'){
                    if(data.messageText){
                        toastr.error(data.messageText);
                    }else{
                        toastr.error('查询失败');
                    }
                }else{
                    toastr.success('账单已发送至邮箱');
                }
            }).error(function(data) {});
        };
        //导出
        $scope.download=function(){
            var params=$scope.ctrlScope.itemsearch;
            $http.post("/admin/account/excel/serviceRebate",params)
                .success(function (data){
                    console.info(data);
                    if(data.result=="success"){
                        window.open(data.file);
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){

                });
        };

    }]);


});